package com.etop.learnapp.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.etop.learnapp.R;
import com.etop.learnapp.adapter.FragmentAdapter;
import com.etop.learnapp.fragment.ChapterFragment;
import com.etop.learnapp.fragment.PracticeFragment;
import com.etop.learnapp.util.MyResId;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by John on 2015/11/7 0007.
 */
public class ChapterActivity extends AppCompatActivity  {
    private Toolbar toolbar;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter);
        MyResId.init(getResources(),this);

        initToolBar();
        initView();
    }

    private void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar_include);
        setSupportActionBar(toolbar);
        setTitle("工程制图");
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }


    private void initView() {

        mTabLayout = (TabLayout) findViewById(R.id.tl_chapter);
        mViewPager = (ViewPager) findViewById(R.id.vp_chapter);
        //初始化TabLayout的title数据集
        List<String> titles = new ArrayList<>();
        titles.add("词典");
        titles.add("练习");
        //初始化TabLayout的title

        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)));
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        //初始化ViewPager的数据集
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new ChapterFragment());
        fragments.add(new PracticeFragment());

        //创建ViewPager的adapter
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        mViewPager.setAdapter(adapter);
    //    mViewPager.setOffscreenPageLimit(3);
        //千万别忘了，关联TabLayout与ViewPager
        //同时也要覆写PagerAdapter的getPageTitle方法，否则Tab没有title
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(adapter);
    }

}
